layui.use(['form', 'table', 'layer'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer;

    table.render({
        elem: '#currentTableId',
        url: '../api/product/pss',
        request: {
            pageName: 'current' //页码的参数名称，默认：page
            , limitName: 'size' //每页数据量的参数名，默认：limit
        },
        cols: [[
            {field: 'pid', title: '编号', sort: true, align: "center"},
            {field: 'sname', title: '产品类别', align: "center"},
            {field: 'pname', title: '产品名', align: "center", fixed: true},
            {field: 'parea', title: '产地', align: "center"},
            {field: 'pml', title: '规格', align: "center"},
            {title: '出入库查询', minWidth: 180, templet: '#currentTablestorquery', fixed: "right", align: "center"},
            {field: 'ksum', title: '剩余数量', sort: true, align: "center"},
            {title: '入库', minWidth: 180, templet: '#currentTablestor', fixed: "right", align: "center"}
        ]],
        limits: [2, 4, 6, 8, 10, 12],
        limit: 4,
        page: true
    });
    // 重载表单
    let reloadTable = (param) => {
        table.reload('currentTableId', {
            url: '../api/product/pss'
            , where: {
                sid: param.sid,
                pid: param.pid,
            }
        });
    };
    //初始化产品类别数据
    $(() => {
        $.get({
            url: "/api/sort/sortList", success: (resp) => {
                resp.forEach(r => {
                    $("#sort").append("<option value='" + r.sid + "'>" + r.sname + "</option>");
                });
                form.render('select');
            }, dataType: "json"
        })
    });
    //初始化产品名称
    form.on('select(sortFilter)', (data) => {
        //部门id
        let sid = $('#sort option:selected').val();
        //根据类别id查询产品名列表
        if (!!sid) {
            $('.e-class').remove();
            $('.c-class').remove();
            $.get({
                url: "/api/product/existp?sid=" + sid, success: (resp) => {
                    resp.forEach(r => {
                        $("#product").append("<option class='e-class' value='" + r.pid + '#' + r.pname + "'>" + r.pname + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
            reloadTable({'sid': sid});
        }
    });
//初始化产品产地
    form.on('select(productFilter)', (data) => {

        let pname = $('#product option:selected').val().split('#')[1];
        let sid = $('#sort option:selected').val();

        if (!!sid) {
            $('.c-class').remove();
            $.get({
                url: "/api/product/productArea?pname=" + pname, success: (resp) => {
                    resp.forEach(r => {
                        $("#producta").append("<option class='c-class' value='" + r.pid + "'>" + r.pArea + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
            reloadTable({'sid': sid, 'pname': pname, 'pid': ''});
        }
    });
    //选中产地
    form.on('select(productaFilter)', (data) => {
        //员工id
        let pid = $('#producta option:selected').val();
        let pname = $('#product option:selected').val().split('#')[0];
        let sid = $('#sort option:selected').val();
        //根据产品名称和id查询产地列表
        reloadTable({'sid': sid, 'pname': pname, 'pid': pid});
        form.render('select');
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'storadd') {
            layer.open({
                type: 2 //此处以iframe举例
                , title: '新增页面'
                , area: ['390px', '400px']
                , shade: 0
                , maxmin: true
                , content: 'pssVorepertoryadd.html'
                , btn: ['提交', '关闭']
                , success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.contents().find('#pname').val(data.sname);
                    body.contents().find('#parea').val(data.parea);
                    body.contents().find('#pId').val(data.pid);
                    form.render();
                }
                , yes: function (index) {
                    debugger;
                    var data = window['layui-layer-iframe' + index].getValue();
                    if (!data.ksum) {
                        layer.msg("入库数量不能为空");
                        return false;
                    }
                    axios.post('/api/product/saveProduct', data).then(function (response) {
                        //post请求执行成功的回调
                        if (response.data.pis == 1) {
                            layer.close(index);
                            //刷新表格
                            tableReload2();
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }).catch(function (error) {
                        console.log(error);
                    });
                }
                , btn2: function (index) {//5
                    layer.close(index);
                }
            })
        }
        if (obj.event === 'storquery') {
            console.log('订单id', data.pid);
            //跳转到订单详情页面
            location.href = '/page/inpProEmpVo.html?pid=' + data.pid;
        }
    })

});